package com.wqnmdb.im.controller;

import com.wqnmdb.im.annotation.PrintData;
import com.wqnmdb.im.domain.contants.ConstantEnum;
import com.wqnmdb.im.domain.req.ReqRegisterUserModel;
import com.wqnmdb.im.domain.req.ReqRegisterAppModel;
import com.wqnmdb.im.domain.resp.R;
import com.wqnmdb.im.exception.ParamsException;
import com.wqnmdb.im.exception.ServiceException;
import com.wqnmdb.im.service.RegisterService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;


import javax.validation.Valid;

@Slf4j
@RestController
@RequestMapping("/register")
@Api(value = "/register", tags = {"注册服务"})
public class RegisterController extends AbstractController {

    @Autowired
    private RegisterService registerService;

    @PrintData
    @PostMapping("/app")
    @ApiOperation(value = "注册APP", notes = "令牌、APP名称必填")
    public ResponseEntity<R> app(@RequestBody @Valid ReqRegisterAppModel model, BindingResult bindingResult) {
        //参数验证~
        checkParams(bindingResult);
        try {
            ResponseEntity<R> result = registerService.app(model.getAuth(), model.getAppName());
            //返回结果~
            return result;
        } catch (Exception ex) {
            throw new ServiceException(ConstantEnum.SERVER_ERR, ex);
        }
    }

    @PrintData
    @PostMapping("/user")
    @ApiOperation(value = "注册用户", notes = "令牌、APP名称必填")
    public ResponseEntity<R> user(@RequestBody @Valid ReqRegisterUserModel model, BindingResult bindingResult) {
        //参数验证~
        checkParams(bindingResult);
        try {
            ResponseEntity<R> result = registerService.user(model.getAccessKey(), model.getSecretKey(), model.getOs(), model.getUserId());
            //返回结果~
            return result;
        } catch (Exception ex) {
            throw new ServiceException(ConstantEnum.SERVER_ERR, ex);
        }
    }
}
